<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>DSpace Documentation : Storage Layer</title>
	    <link rel="stylesheet" href="styles/site.css" type="text/css" />
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">	    
    </head>

    <body>
	    <table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
		    <tr>
			    <td valign="top" class="pagebody">
				    <div class="pageheader">
					    <span class="pagetitle">
                            DSpace Documentation : Storage Layer
                                                    </span>
				    </div>
				    <div class="pagesubheading">
					    This page last changed on Feb 17, 2011 by <font color="#0050B2">helix84</font>.
				    </div>

				    <h1><a name="StorageLayer-SystemArchitecture%3AStorageLayer"></a>System Architecture: Storage Layer</h1>

<p>In this section, we explain the storage layer: the database structure, maintenance, and the bistream store and configurations.</p>

<style type='text/css'>/*<![CDATA[*/
div.rbtoc1297952020967 {margin-left: 0px;padding: 0px;}
div.rbtoc1297952020967 ul {list-style: none;margin-left: 0px;}
div.rbtoc1297952020967 li {margin-left: 0px;padding-left: 0px;}

/*]]>*/</style><div class='rbtoc1297952020967'>
<ul>
    <li><span class='TOCOutline'>1</span> <a href='#StorageLayer-RDBMS%2FDatabaseStructure'>RDBMS / Database Structure</a></li>
<ul>
    <li><span class='TOCOutline'>1.1</span> <a href='#StorageLayer-MaintenanceandBackup'>Maintenance and Backup</a></li>
    <li><span class='TOCOutline'>1.2</span> <a href='#StorageLayer-ConfiguringtheRDBMSComponent'>Configuring the RDBMS Component</a></li>
</ul>
    <li><span class='TOCOutline'>2</span> <a href='#StorageLayer-BitstreamStore'>Bitstream Store</a></li>
<ul>
    <li><span class='TOCOutline'>2.1</span> <a href='#StorageLayer-Backup'>Backup</a></li>
    <li><span class='TOCOutline'>2.2</span> <a href='#StorageLayer-ConfiguringtheBitstreamStore'>Configuring the Bitstream Store</a></li>
<ul>
    <li><span class='TOCOutline'>2.2.1</span> <a href='#StorageLayer-ConfiguringTraditionalStorage'>Configuring Traditional Storage</a></li>
    <li><span class='TOCOutline'>2.2.2</span> <a href='#StorageLayer-ConfiguringSRBStorage'>Configuring SRB Storage</a></li>
</ul>
</ul>
</ul></div>

<h2><a name="StorageLayer-RDBMS%2FDatabaseStructure"></a>RDBMS / Database Structure</h2>

<p>DSpace uses a relational database to store all information about the organization of content, metadata about the content, information about e-people and authorization, and the state of currently-running workflows. The DSpace system also uses the relational database in order to maintain indices that users can browse.</p>

<p><span class="image-wrap" style=""><img src="attachments/22022822/21954891.png" style="border: 1px solid black"/></span></p>

<p>Most of the functionality that DSpace uses can be offered by any standard SQL database that supports transactions. Presently, the browse indices use some features specific to <a href="http://www.postgresql.org/" title="PostgreSQL">PostgreSQL</a> and <a href="http://www.oracle.com/database/" title="Oracle">Oracle</a>, so some modification to the code would be needed before DSpace would function fully with an alternative database back-end.</p>

<p>The <em>org.dspace.storage.rdbms</em> package provides access to an SQL database in a somewhat simpler form than using JDBC directly. The main class is <em>DatabaseManager</em>, which executes SQL queries and returns <em>TableRow</em> or <em>TableRowIterator</em> objects. The <em>InitializeDatabase</em> class is used to load SQL into the database via JDBC, for example to set up the schema.</p>

<p>All calls to the <em>Database Manager</em> require a DSpace <em>Context</em> object. Example use of the database manager API is given in the <em>org.dspace.storage.rdbms</em> package Javadoc.</p>

<p>The database schema used by DSpace is created by SQL statements stored in a directory specific to each supported RDBMS platform:</p>

<ul>
	<li>PostgreSQL schemas are in <em>[dspace-source]/dspace/etc/postgres/</em></li>
	<li>Oracle schemas are in <em>[dspace-source]/dspace/etc/oracle/</em><br/>
The SQL (DDL) statements to create the tables for the current release, starting with an empty database, aer in <em>database_schema.sql</em>. The schema SQL file also creates the two required e-person groups (<em>Anonymous</em> and <em>Administrator</em>) that are required for the system to function properly.</li>
</ul>


<p>Also in <em>[dspace-source]/dspace/etc/[database]</em> are various SQL files called <em>database_schema_1x_1y</em>. These contain the necessary SQL commands to update a live DSpace database from version 1.<em>x</em> to 1.<em>y</em>. Note that this might not be the only part of an upgrade process: see Updating a DSpace Installation for details.</p>

<p>The DSpace database code uses an SQL function <em>getnextid</em> to assign primary keys to newly created rows. This SQL function must be safe to use if several JVMs are accessing the database at once; for example, the Web UI might be creating new rows in the database at the same time as the batch item importer. The PostgreSQL-specific implementation of the method uses <em>SEQUENCES</em> for each table in order to create new IDs. If an alternative database backend were to be used, the implementation of <em>getnextid</em> could be updated to operate with that specific DBMS.</p>

<p>The <em>etc</em> directory in the source distribution contains two further SQL files. <em>clean-database.sql</em> contains the SQL necessary to completely clean out the database, so use with caution&#33; The Ant target <em>clean_database</em> can be used to execute this. <em>update-sequences.sql</em> contains SQL to reset the primary key generation sequences to appropriate values. You'd need to do this if, for example, you're restoring a backup database dump which creates rows with specific primary keys already defined. In such a case, the sequences would allocate primary keys that were already used.</p>

<p>Versions of the <b><em>.sql</em></b> files for Oracle are stored in <em>[dspace-source]/dspace/etc/oracle</em>. These need to be copied over their PostgreSQL counterparts in <em>[dspace-source]/dspace/etc</em> prior to installation.</p>

<h3><a name="StorageLayer-MaintenanceandBackup"></a>Maintenance and Backup</h3>

<p>When using PostgreSQL, it's a good idea to perform regular 'vacuuming' of the database to optimize performance. This is performed by the <em>vacuumdb</em> command which can be executed via a 'cron' job, for example by putting this in the system <em>crontab</em>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
# clean up the database nightly
40 2 * * * /usr/local/pgsql/bin/vacuumdb --analyze dspace &gt; /dev/<span class="code-keyword">null</span> 2&gt;&amp;1
</pre>
</div></div>
<p>The DSpace database can be backed up and restored using usual methods, for example with <em>pg_dump</em> and <em>psql</em>. However when restoring a database, you will need to perform these additional steps:</p>

<ul>
	<li>The <em>fresh_install</em> target loads up the initial contents of the Dublin Core type and bitstream format registries, as well as two entries in the <em>epersongroup</em> table for the system anonymous and administrator groups. Before you restore a raw backup of your database you will need to remove these, since they will already exist in your backup, possibly having been modified. For example, use:
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
DELETE FROM dctyperegistry;
DELETE FROM bitstreamformatregistry;
DELETE FROM epersongroup;
</pre>
</div></div></li>
	<li>After restoring a backup, you will need to reset the primary key generation sequences so that they do not produce already-used primary keys. Do this by executing the SQL in <em>[dspace-source]/dspace/etc/update-sequences.sql</em>, for example with:
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
psql -U dspace -f  [dspace-source]/dspace/etc/update-sequences.sql
</pre>
</div></div>
<p>Future updates of DSpace may involve minor changes to the database schema. Specific instructions on how to update the schema whilst keeping live data will be included. The current schema also contains a few currently unused database columns, to be used for extra functionality in future releases. These unused columns have been added in advance to minimize the effort required to upgrade.</p></li>
</ul>



<h3><a name="StorageLayer-ConfiguringtheRDBMSComponent"></a>Configuring the RDBMS Component</h3>

<p>The database manager is configured with the following properties in <em>dspace.cfg</em>:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<td class='confluenceTd'> db.url </td>
<td class='confluenceTd'> The JDBC URL to use for accessing the database. This should not point to a connection pool, since DSpace already implements a connection pool. </td>
</tr>
<tr>
<td class='confluenceTd'> db.driver </td>
<td class='confluenceTd'> JDBC driver class name. Since presently, DSpace uses PostgreSQL-specific features, this should be <em>org.postgresql.Driver</em>. </td>
</tr>
<tr>
<td class='confluenceTd'> db.username </td>
<td class='confluenceTd'> Username to use when accessing the database. </td>
</tr>
<tr>
<td class='confluenceTd'> db.password </td>
<td class='confluenceTd'> Corresponding password ot use when accessing the database. </td>
</tr>
</tbody></table>
</div>




<h2><a name="StorageLayer-BitstreamStore"></a>Bitstream Store</h2>

<p>DSpace offers two means for storing content. The first is in the file system on the server. The second is using <a href="http://www.sdsc.edu/srb" title="SRB (Storage Resource Broker)">SRB (Storage Resource Broker)</a>. Both are achieved using a simple, lightweight API.</p>

<p>SRB is purely an option but may be used in lieu of the server's file system or in addition to the file system. Without going into a full description, SRB is a very robust, sophisticated storage manager that offers essentially unlimited storage and straightforward means to replicate (in simple terms, backup) the content on other local or remote storage resources.</p>

<p>The terms "store", "retrieve", "in the system", "storage", and so forth, used below can refer to storage in the file system on the server ("traditional") or in SRB.</p>

<p>The <em>BitstreamStorageManager</em> provides low-level access to bitstreams stored in the system. In general, it should not be used directly; instead, use the <em>Bitstream</em> object in the content management API since that encapsulated authorization and other metadata to do with a bitstream that are not maintained by the <em>BitstreamStorageManager</em>.</p>

<p>The bitstream storage manager provides three methods that store, retrieve and delete bitstreams. Bitstreams are referred to by their 'ID'; that is the primary key <em>bitstream_id</em> column of the corresponding row in the database.</p>

<p>As of DSpace version 1.1, there can be multiple bitstream stores. Each of these bitstream stores can be traditional storage or SRB storage. This means that the potential storage of a DSpace system is not bound by the maximum size of a single disk or file system and also that traditional and SRB storage can be combined in one DSpace installation. Both traditional and SRB storage are specified by configuration parameters. Also see Configuring the Bitstream Store below.</p>

<p>Stores are numbered, starting with zero, then counting upwards. Each bitstream entry in the database has a store number, used to retrieve the bitstream when required.</p>

<p>At the moment, the store in which new bitstreams are placed is decided using a configuration parameter, and there is no provision for moving bitstreams between stores. Administrative tools for manipulating bitstreams and stores will be provided in future releases. Right now you can move a whole store (e.g. you could move store number 1 from <em>/localdisk/store</em> to <em>/fs/anotherdisk/store</em> but it would still have to be store number 1 and have the exact same contents.</p>

<p>Bitstreams also have an 38-digit internal ID, different from the primary key ID of the bitstream table row. This is not visible or used outside of the bitstream storage manager. It is used to determine the exact location (relative to the relevant store directory) that the bitstream is stored in traditional or SRB storage. The first three pairs of digits are the directory path that the bitstream is stored under. The bitstream is stored in a file with the internal ID as the filename.</p>

<p>For example, a bitstream with the internal ID <em>12345678901234567890123456789012345678</em> is stored in the directory:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
(assetstore dir)/12/34/56/12345678901234567890123456789012345678
</pre>
</div></div>
<p>The reasons for storing files this way are:</p>

<ul>
	<li>Using a randomly-generated 38-digit number means that the 'number space' is less cluttered than simply using the primary keys, which are allocated sequentially and are thus close together. This means that the bitstreams in the store are distributed around the directory structure, improving access efficiency.</li>
	<li>The internal ID is used as the filename partly to avoid requiring an extra lookup of the filename of the bitstream, and partly because bitstreams may be received from a variety of operating systems. The original name of a bitstream may be an illegal UNIX filename.<br/>
When storing a bitstream, the <em>BitstreamStorageManager</em> DOES set the following fields in the corresponding database table row:</li>
</ul>


<ul>
	<li><em>bitstream_id</em></li>
	<li><em>size</em></li>
	<li><em>checksum</em></li>
	<li><em>checksum_algorithm</em></li>
	<li><em>internal_id</em></li>
	<li><em>deleted</em></li>
	<li><em>store_number</em><br/>
The remaining fields are the responsibility of the <em>Bitstream</em> content management API class.</li>
</ul>


<p>The bitstream storage manager is fully transaction-safe. In order to implement transaction-safety, the following algorithm is used to store bitstreams:</p>

<ol>
	<li>A database connection is created, separately from the currently active connection in the current DSpace context.</li>
	<li>An unique internal identifier (separate from the database primary key) is generated.</li>
	<li>The bitstream DB table row is created using this new connection, with the <em>deleted</em> column set to <em>true</em>.</li>
	<li>The new connection is &#95;commit_ted, so the 'deleted' bitstream row is written to the database</li>
	<li>The bitstream itself is stored in a file in the configured 'asset store directory', with a directory path and filename derived from the internal ID</li>
	<li>The <em>deleted</em> flag in the bitstream row is set to <em>false</em>. This will occur (or not) as part of the current DSpace <em>Context</em>.<br/>
This means that should anything go wrong before, during or after the bitstream storage, only one of the following can be true:</li>
</ol>


<ul>
	<li>No bitstream table row was created, and no file was stored</li>
	<li>A bitstream table row with <em>deleted=true</em> was created, no file was stored</li>
	<li>A bitstream table row with <em>deleted=true</em> was created, and a file was stored<br/>
None of these affect the integrity of the data in the database or bitstream store.</li>
</ul>


<p>Similarly, when a bitstream is deleted for some reason, its <em>deleted</em> flag is set to true as part of the overall transaction, and the corresponding file in storage is <em>not</em> deleted.</p>

<p>The above techniques mean that the bitstream storage manager is transaction-safe. Over time, the bitstream database table and file store may contain a number of 'deleted' bitstreams. The <em>cleanup</em> method of <em>BitstreamStorageManager</em> goes through these deleted rows, and actually deletes them along with any corresponding files left in the storage. It only removes 'deleted' bitstreams that are more than one hour old, just in case cleanup is happening in the middle of a storage operation.</p>

<p>This cleanup can be invoked from the command line via the <em>Cleanup</em> class, which can in turn be easily executed from a shell on the server machine using <em>/dspace/bin/cleanup</em>. You might like to have this run regularly by <em>cron</em>, though since DSpace is read-lots, write-not-so-much it doesn't need to be run very often.</p>

<h3><a name="StorageLayer-Backup"></a>Backup</h3>

<p>The bitstreams (files) in traditional storage may be backed up very easily by simply 'tarring' or 'zipping' the <em>assetstore</em> directory (or whichever directory is configured in <em>dspace.cfg</em>). Restoring is as simple as extracting the backed-up compressed file in the appropriate location.</p>

<p>Similar means could be used for SRB, but SRB offers many more options for managing backup.</p>

<p>It is important to note that since the bitstream storage manager holds the bitstreams in storage, and information about them in the database, that a database backup and a backup of the files in the bitstream store must be made at the same time; the bitstream data in the database must correspond to the stored files.</p>

<p>Of course, it isn't really ideal to 'freeze' the system while backing up to ensure that the database and files match up. Since DSpace uses the bitstream data in the database as the authoritative record, it's best to back up the database before the files. This is because it's better to have a bitstream in storage but not the database (effectively non-existent to DSpace) than a bitstream record in the database but not storage, since people would be able to find the bitstream but not actually get the contents.</p>

<p>With DSpace 1.7 and above, there is also the option to backup both files and metadata via the <a href="AIP Backup and Restore.html" title="AIP Backup and Restore">AIP Backup and Restore</a> feature.</p>

<h3><a name="StorageLayer-ConfiguringtheBitstreamStore"></a>Configuring the Bitstream Store</h3>

<p>Both traditional and SRB bitstream stores are configured in <em>dspace.cfg</em>.</p>

<h4><a name="StorageLayer-ConfiguringTraditionalStorage"></a>Configuring Traditional Storage</h4>

<p>Bitstream stores in the file system on the server are configured like this:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
assetstore.dir =  [dspace]/assetstore
</pre>
</div></div>
<p>(Remember that <em>[dspace]</em> is a placeholder for the actual name of your DSpace install directory).</p>

<p>The above example specifies a single asset store.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
assetstore.dir =  [dspace]/assetstore_0
assetstore.dir.1 = /mnt/other_filesystem/assetstore_1
</pre>
</div></div>
<p>The above example specifies two asset stores. assetstore.dir specifies the asset store number 0 (zero); after that use assetstore.dir.1, assetstore.dir.2 and so on. The particular asset store a bitstream is stored in is held in the database, so don't move bitstreams between asset stores, and don't renumber them.</p>

<p>By default, newly created bitstreams are put in asset store 0 (i.e. the one specified by the assetstore.dir property.) This allows backwards compatibility with pre-DSpace 1.1 configurations. To change this, for example when asset store 0 is getting full, add a line to <em>dspace.cfg</em> like:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
assetstore.incoming = 1
</pre>
</div></div>
<p>Then restart DSpace (Tomcat). New bitstreams will be written to the asset store specified by <em>assetstore.dir.1</em>, which is <em>/mnt/other_filesystem/assetstore_1</em> in the above example.</p>


<h4><a name="StorageLayer-ConfiguringSRBStorage"></a>Configuring SRB Storage</h4>

<p>The same framework is used to configure SRB storage. That is, the asset store number (0..n) can reference a file system directory as above or it can reference a set of SRB account parameters. But any particular asset store number can reference one or the other but not both. This way traditional and SRB storage can both be used but with different asset store numbers. The same cautions mentioned above apply to SRB asset stores as well: The particular asset store a bitstream is stored in is held in the database, so don't move bitstreams between asset stores, and don't renumber them.</p>

<p>For example, let's say asset store number 1 will refer to SRB. The there will be a set of SRB account parameters like this:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
srb.host.1 = mysrbmcathost.myu.edu
srb.port.1 = 5544
srb.mcatzone.1 = mysrbzone
srb.mdasdomainname.1 = mysrbdomain
srb.defaultstorageresource.1 = mydefaultsrbresource
srb.username.1 = mysrbuser
srb.password.1 = mysrbpassword
srb.homedirectory.1 = /mysrbzone/home/mysrbuser.mysrbdomain
srb.parentdir.1 = mysrbdspaceassetstore
</pre>
</div></div>
<p>Several of the terms, such as <em>mcatzone</em>, have meaning only in the SRB context and will be familiar to SRB users. The last, <em>srb.parentdir.n</em>, can be used to used for addition (SRB) upper directory structure within an SRB account. This property value could be blank as well.</p>

<p>(If asset store 0 would refer to SRB it would be <em>srb.host =</em> ..., <em>srb.port =</em> ..., and so on (<em>.0</em> omitted) to be consistent with the traditional storage configuration above.)</p>

<p>The similar use of <em>assetstore.incoming</em> to reference asset store 0 (default) or 1..n (explicit property) means that new bitstreams will be written to traditional or SRB storage determined by whether a file system directory on the server is referenced or a set of SRB account parameters are referenced.</p>

<p>There are comments in dspace.cfg that further elaborate the configuration of traditional and SRB storage.</p>

				    					    <br/>
                        <div class="tabletitle">
                            <a name="attachments">Attachments:</a>
                        </div>

                        <div class="greybox" align="left">
                                                            <img src="images/icons/bullet_blue.gif" height="8" width="8" alt=""/>
                                <a href="attachments/22022822/21954862.gif">db-schema.gif</a> (image/gif)
                                <br/>
                                                            <img src="images/icons/bullet_blue.gif" height="8" width="8" alt=""/>
                                <a href="attachments/22022822/21954891.png">db-schema.png</a> (image/png)
                                <br/>
                                                    </div>
				    
                    			    </td>
		    </tr>
	    </table>
	    <table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td height="12" background="https://wiki.duraspace.org/images/border/border_bottom.gif"><img src="images/border/spacer.gif" width="1" height="1" border="0"/></td>
			</tr>
		    <tr>
			    <td align="center"><font color="grey">Document generated by Confluence on Mar 25, 2011 19:21</font></td>
		    </tr>
	    </table>
    </body>
</html>